//使用说明：
//npm install  或 npm i 
//node index.js  test.t_userinfo 
//到当前目录下查看对应的json文件
var mysql = require('mysql');
// http://nodejs.org/docs/v0.6.5/api/fs.html#fs.writeFile
var fs = require('fs');
if(process.argv.length<3 || process.argv[2].length==0){
	console.error('请输入表名');
	return;
}
var tableName = process.argv[2];
var connection = mysql.createConnection({
   host: 'localhost',
   user: 'root',
   password: '123'
});

connection.connect();
connection.query('select * from '+tableName + ';', function(err, results, fields) {
    if(err) throw err;
	var json = 'table_'+tableName+'.json';
    fs.writeFile(json, JSON.stringify(results), function (err) {
      if (err) throw err;
      console.log('已经保存到文件：%s  共有记录数 %d条',json ,results.length );
    }); 
    connection.end();
});